<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one or more</a>
<span class="sourceLineNo">003</span><a id="line.3"> * contributor license agreements.  See the NOTICE file distributed with</a>
<span class="sourceLineNo">004</span><a id="line.4"> * this work for additional information regarding copyright ownership.</a>
<span class="sourceLineNo">005</span><a id="line.5"> * The ASF licenses this file to You under the Apache License, Version 2.0</a>
<span class="sourceLineNo">006</span><a id="line.6"> * (the "License"); you may not use this file except in compliance with</a>
<span class="sourceLineNo">007</span><a id="line.7"> * the License.  You may obtain a copy of the License at</a>
<span class="sourceLineNo">008</span><a id="line.8"> *</a>
<span class="sourceLineNo">009</span><a id="line.9"> *      http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">010</span><a id="line.10"> *</a>
<span class="sourceLineNo">011</span><a id="line.11"> * Unless required by applicable law or agreed to in writing, software</a>
<span class="sourceLineNo">012</span><a id="line.12"> * distributed under the License is distributed on an "AS IS" BASIS,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</a>
<span class="sourceLineNo">014</span><a id="line.14"> * See the License for the specific language governing permissions and</a>
<span class="sourceLineNo">015</span><a id="line.15"> * limitations under the License.</a>
<span class="sourceLineNo">016</span><a id="line.16"> */</a>
<span class="sourceLineNo">017</span><a id="line.17">package org.apache.commons.dbutils.handlers;</a>
<span class="sourceLineNo">018</span><a id="line.18"></a>
<span class="sourceLineNo">019</span><a id="line.19">import java.sql.ResultSet;</a>
<span class="sourceLineNo">020</span><a id="line.20">import java.sql.SQLException;</a>
<span class="sourceLineNo">021</span><a id="line.21">import java.util.HashMap;</a>
<span class="sourceLineNo">022</span><a id="line.22">import java.util.Map;</a>
<span class="sourceLineNo">023</span><a id="line.23"></a>
<span class="sourceLineNo">024</span><a id="line.24">import org.apache.commons.dbutils.ResultSetHandler;</a>
<span class="sourceLineNo">025</span><a id="line.25"></a>
<span class="sourceLineNo">026</span><a id="line.26">/**</a>
<span class="sourceLineNo">027</span><a id="line.27"> * &lt;p&gt;</a>
<span class="sourceLineNo">028</span><a id="line.28"> * {@code ResultSetHandler} implementation that returns a Map.</a>
<span class="sourceLineNo">029</span><a id="line.29"> * {@code ResultSet} rows are converted into objects (Vs) which are then stored</a>
<span class="sourceLineNo">030</span><a id="line.30"> * in a Map under the given keys (Ks).</a>
<span class="sourceLineNo">031</span><a id="line.31"> * &lt;/p&gt;</a>
<span class="sourceLineNo">032</span><a id="line.32"> *</a>
<span class="sourceLineNo">033</span><a id="line.33"> * @param &lt;K&gt; the type of keys maintained by the returned map</a>
<span class="sourceLineNo">034</span><a id="line.34"> * @param &lt;V&gt; the type of mapped values</a>
<span class="sourceLineNo">035</span><a id="line.35"> * @see org.apache.commons.dbutils.ResultSetHandler</a>
<span class="sourceLineNo">036</span><a id="line.36"> * @since DbUtils 1.3</a>
<span class="sourceLineNo">037</span><a id="line.37"> */</a>
<span class="sourceLineNo">038</span><a id="line.38">public abstract class AbstractKeyedHandler&lt;K, V&gt; implements ResultSetHandler&lt;Map&lt;K, V&gt;&gt; {</a>
<span class="sourceLineNo">039</span><a id="line.39"></a>
<span class="sourceLineNo">040</span><a id="line.40"></a>
<span class="sourceLineNo">041</span><a id="line.41">    /**</a>
<span class="sourceLineNo">042</span><a id="line.42">     * Convert each row's columns into a Map and store then</a>
<span class="sourceLineNo">043</span><a id="line.43">     * in a {@code Map} under {@code ResultSet.getObject(key)} key.</a>
<span class="sourceLineNo">044</span><a id="line.44">     * @param rs {@code ResultSet} to process.</a>
<span class="sourceLineNo">045</span><a id="line.45">     * @return A {@code Map}, never {@code null}.</a>
<span class="sourceLineNo">046</span><a id="line.46">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">047</span><a id="line.47">     * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)</a>
<span class="sourceLineNo">048</span><a id="line.48">     */</a>
<span class="sourceLineNo">049</span><a id="line.49">    @Override</a>
<span class="sourceLineNo">050</span><a id="line.50">    public Map&lt;K, V&gt; handle(final ResultSet rs) throws SQLException {</a>
<span class="sourceLineNo">051</span><a id="line.51">        final Map&lt;K, V&gt; result = createMap();</a>
<span class="sourceLineNo">052</span><a id="line.52">        while (rs.next()) {</a>
<span class="sourceLineNo">053</span><a id="line.53">            result.put(createKey(rs), createRow(rs));</a>
<span class="sourceLineNo">054</span><a id="line.54">        }</a>
<span class="sourceLineNo">055</span><a id="line.55">        return result;</a>
<span class="sourceLineNo">056</span><a id="line.56">    }</a>
<span class="sourceLineNo">057</span><a id="line.57"></a>
<span class="sourceLineNo">058</span><a id="line.58">    /**</a>
<span class="sourceLineNo">059</span><a id="line.59">     * This factory method is called by {@code handle()} to create the Map</a>
<span class="sourceLineNo">060</span><a id="line.60">     * to store records in.  This implementation returns a {@code HashMap}</a>
<span class="sourceLineNo">061</span><a id="line.61">     * instance.</a>
<span class="sourceLineNo">062</span><a id="line.62">     *</a>
<span class="sourceLineNo">063</span><a id="line.63">     * @return Map to store records in</a>
<span class="sourceLineNo">064</span><a id="line.64">     */</a>
<span class="sourceLineNo">065</span><a id="line.65">    protected Map&lt;K, V&gt; createMap() {</a>
<span class="sourceLineNo">066</span><a id="line.66">        return new HashMap&lt;&gt;();</a>
<span class="sourceLineNo">067</span><a id="line.67">    }</a>
<span class="sourceLineNo">068</span><a id="line.68"></a>
<span class="sourceLineNo">069</span><a id="line.69">    /**</a>
<span class="sourceLineNo">070</span><a id="line.70">     * This factory method is called by {@code handle()} to retrieve the</a>
<span class="sourceLineNo">071</span><a id="line.71">     * key value from the current {@code ResultSet} row.</a>
<span class="sourceLineNo">072</span><a id="line.72">     * @param rs ResultSet to create a key from</a>
<span class="sourceLineNo">073</span><a id="line.73">     * @return K from the configured key column name/index</a>
<span class="sourceLineNo">074</span><a id="line.74">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">075</span><a id="line.75">     */</a>
<span class="sourceLineNo">076</span><a id="line.76">    protected abstract K createKey(ResultSet rs) throws SQLException;</a>
<span class="sourceLineNo">077</span><a id="line.77"></a>
<span class="sourceLineNo">078</span><a id="line.78">    /**</a>
<span class="sourceLineNo">079</span><a id="line.79">     * This factory method is called by {@code handle()} to store the</a>
<span class="sourceLineNo">080</span><a id="line.80">     * current {@code ResultSet} row in some object.</a>
<span class="sourceLineNo">081</span><a id="line.81">     * @param rs ResultSet to create a row from</a>
<span class="sourceLineNo">082</span><a id="line.82">     * @return V object created from the current row</a>
<span class="sourceLineNo">083</span><a id="line.83">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">084</span><a id="line.84">     */</a>
<span class="sourceLineNo">085</span><a id="line.85">    protected abstract V createRow(ResultSet rs) throws SQLException;</a>
<span class="sourceLineNo">086</span><a id="line.86"></a>
<span class="sourceLineNo">087</span><a id="line.87">}</a>




























































</pre>
</div>
</main>
</body>
</html>
